package com.yotoodesign.dianba.fragment;


import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.yotoodesign.core.util.NetUtil;
import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.ui.MainActivity;
import com.yotoodesign.dianba.util.Constant;
import com.yotoodesign.dianba.util.DownloadUtil;


public class MoreFragment extends BaseFragment {

	private FeedbackFragment feedbackFragment;
	private AboutFragment aboutFragment;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.more_fragment, container,false);
		TextView title = (TextView)view.findViewById(R.id.mainTitle);
		title.setText("更多");
		
		ImageButton commonBack = (ImageButton)view.findViewById(R.id.commonBack);
		commonBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				GlobalApplication.getMainActivity().goHome();
			}
		});
		
		Button btnExit = (Button)view.findViewById(R.id.btnExit);
		btnExit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				System.exit(0);
			}
		});
		
		Button btnSync = (Button)view.findViewById(R.id.btnSync);
		btnSync.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new DownloadUtil(getActivity()).showDownloadDialog("同步歌曲库",10001);
				
			}
		});
		
		Button btnFeedback = (Button)view.findViewById(R.id.btnFeedback);
		btnFeedback.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showFeedback(Constant.TAG_FEEDBACK);
			}
		});
		
		Button btnAbout = (Button)view.findViewById(R.id.btnAbout);
		btnAbout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showAbout(Constant.TAG_ABOUT);
			}
		});
		
		Button btnCheckVersion = (Button)view.findViewById(R.id.btnCheckVersion);
		btnCheckVersion.setText(getString(R.string.check_version)+"(ver "+DownloadUtil.currentVer+")");
		btnCheckVersion.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new DownloadUtil(getActivity()).showDownloadDialog("版本检查",10002);
				
			}
		});
		
		return view;
	}

	@Override
	public void updateListView() {
		
	}
	
	/*
	 * 显示用户意见反馈输入界面
	 */
	private void showFeedback(String tabId){
		FragmentManager fm = getActivity().getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		//先detach主Fragment
		ft.detach(this);
		feedbackFragment = (FeedbackFragment)fm.findFragmentByTag(tabId);
		if(feedbackFragment==null){
			try {
				feedbackFragment = new FeedbackFragment();
				ft.add(R.id.realtabcontent,feedbackFragment, tabId);
				//添加到后退堆栈里
				ft.addToBackStack(tabId);
			} catch (Exception e) {
				Log.i(Constant.DIANBA,"add to back stack error");
			}						
		}else{
			ft.attach(feedbackFragment);						
		}
		
		ft.commit();
		
		MainActivity mainActivity = (MainActivity)this.getActivity();
		mainActivity.setCurrentFragment(feedbackFragment);
		mainActivity.showTab(false);
	}
	
	/*
	 * 显示关于界面
	 */
	private void showAbout(String tabId){
		FragmentManager fm = getActivity().getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		//先detach主Fragment
		ft.detach(this);
		aboutFragment = (AboutFragment)fm.findFragmentByTag(tabId);
		if(aboutFragment==null){
			try {
				aboutFragment = new AboutFragment();
				ft.add(R.id.realtabcontent,aboutFragment, tabId);
				//添加到后退堆栈里
				ft.addToBackStack(tabId);
			} catch (Exception e) {
				Log.i(Constant.DIANBA,"add to back stack error");
			}						
		}else{
			ft.attach(aboutFragment);						
		}
		
		ft.commit();
		
		MainActivity mainActivity = (MainActivity)this.getActivity();
		mainActivity.setCurrentFragment(aboutFragment);
	}

	@Override
	public void onResume() {
		super.onResume();
		MainActivity mainActivity = (MainActivity)this.getActivity();
		mainActivity.showTab(true);
	}
	
	

}
